/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.servicerecord;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.member.Member;
import com.server.modules.sys.entity.servicerecord.ServiceRecord;
import com.server.modules.sys.service.servicerecord.ServiceRecordService;

/**
 * 服务记录查询Controller
 * @author 王磊
 * @version 2016-09-19
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/servicerecord/serviceRecord")
public class ServiceRecordController extends BaseController {

	@Autowired
	private ServiceRecordService serviceRecordService;
	
	@ModelAttribute
	public ServiceRecord get(@RequestParam(required=false) String id) {
		ServiceRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = serviceRecordService.get(id);
		}
		if (entity == null){
			entity = new ServiceRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:servicerecord:serviceRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(ServiceRecord serviceRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ServiceRecord> page = serviceRecordService.findPage(new Page<ServiceRecord>(request, response), serviceRecord); 
		model.addAttribute("page", page);
		model.addAttribute("serviceRecord", serviceRecord);
		return "modules/sys/servicerecord/serviceRecordList";
	}

	@RequiresPermissions("sys:servicerecord:serviceRecord:view")
	@RequestMapping(value = "form")
	public String form(ServiceRecord serviceRecord, Model model) {
		model.addAttribute("serviceRecord", serviceRecord);
		return "modules/sys/servicerecord/serviceRecordForm";
	}

	@RequiresPermissions("sys:servicerecord:serviceRecord:edit")
	@RequestMapping(value = "save")
	public String save(ServiceRecord serviceRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, serviceRecord)){
			return form(serviceRecord, model);
		}
		serviceRecordService.save(serviceRecord);
		addMessage(redirectAttributes, "保存服务记录成功");
		return "redirect:"+Global.getAdminPath()+"/sys/servicerecord/serviceRecord/?repage";
	}
	
	@RequiresPermissions("sys:servicerecord:serviceRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(ServiceRecord serviceRecord, RedirectAttributes redirectAttributes) {
		serviceRecordService.delete(serviceRecord);
		addMessage(redirectAttributes, "删除服务记录成功");
		return "redirect:"+Global.getAdminPath()+"/sys/servicerecord/serviceRecord/?repage";
	}
	
	@RequiresPermissions("sys:member:member:view")
	@RequestMapping(value = "ajaxSave")
	@ResponseBody
	public Object ajaxSave(ServiceRecord serviceRecord, Model model) {
		Member member = serviceRecord.getMember();
		if(member==null||StringUtils.isBlank(member.getId())){
			return false;
		}
		serviceRecordService.save(serviceRecord);
		return true;
	}
	@RequiresPermissions("sys:member:member:view")
	@RequestMapping(value = "ajaxDelete")
	@ResponseBody
	public Object ajaxDelete(ServiceRecord serviceRecord) {
		if(serviceRecord==null||StringUtils.isBlank(serviceRecord.getId())){
			return false;
		}
		serviceRecordService.delete(serviceRecord);
		return true;
	}

}